package www.rlicker.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;

import www.rlicker.model.DepositRecord;
import www.rlicker.model.GroupResource;
import www.rlicker.model.SysRole;
import www.rlicker.model.UserMoney;
import www.rlicker.mybatis.Criteria;
import www.rlicker.mybatis.PageResult;
import www.rlicker.service.IDepositRecordService;
import www.rlicker.service.IGroupResourceService;
import www.rlicker.service.ISysRoleService;
import www.rlicker.service.IUserMoneyService;
import www.rlicker.utils.BaseController;
import www.rlicker.utils.JsonResult;
import www.rlicker.utils.StringUtil;

@Controller
@RequestMapping(value="deposit")
public class DepositRecordController extends BaseController{

	@Autowired
	private IDepositRecordService service;
	
	@Autowired
	private ISysRoleService sysRoleService;
	
	@Autowired
	private IGroupResourceService groupResourceService;
	
	@Autowired
	private IUserMoneyService moneyService;
	
	@RequestMapping(value="showlist")
	public String showList(ModelMap map, HttpServletRequest request){
		SysRole sysRole = sysRoleService.queryByRoleKey(getUserId(request));
		if (sysRole != null) {
			Criteria<GroupResource> params = new Criteria<GroupResource>();
			params.addParam("resourceIdList", getResourceId(sysRole.getResourceId()));
			params.addParam("parent_resource_id",new Long(request.getParameter("id")));
			params.addParam("status", 0);
			params.addParam("showType", 1);
			List<GroupResource> lists = groupResourceService.queryPageList(params);
            map.put("gpLists", JSONArray.toJSONString(lists));
		}
        map.put("id", request.getParameter("id"));
		return "deposit/depositRecordList";
	}
	
	@RequestMapping("/querypage")
	@ResponseBody
	public PageResult queryPage(HttpServletRequest request) {
		Criteria<DepositRecord> param = new Criteria<DepositRecord>();
		if (!StringUtil.isNullOrBlank(request.getParameter("startTime"))) {
			param.addParam("startTime", request.getParameter("startTime"));
		}
		if (!StringUtil.isNullOrBlank(request.getParameter("endTime"))) {
			param.addParam("endTime", request.getParameter("endTime"));
		}
		if (!StringUtil.isNullOrBlank(request.getParameter("startMoney"))) {
			param.addParam("startMoney", request.getParameter("startMoney"));
		}
		if (!StringUtil.isNullOrBlank(request.getParameter("endMoney"))) {
			param.addParam("endMoney", request.getParameter("endMoney"));
		}
		if (!StringUtil.isNullOrBlank(request.getParameter("phone"))) {
			param.addParam("phone", request.getParameter("phone"));
		}
		if (!StringUtil.isNullOrBlank(request.getParameter("status"))) {
			param.addParam("status", request.getParameter("status"));
		}
	    if (!StringUtil.isNullOrBlank(request.getParameter("pageNumber"))) {
	    	param.addParam("startIndex", Integer.parseInt(request.getParameter("pageNumber")));
	    }
	    if (!StringUtil.isNullOrBlank(request.getParameter("pageSize"))) {
	    	param.addParam("endPage", Integer.parseInt(request.getParameter("pageSize")));
	    }
		List<Map<String, Object>> list = service.queryPage(param);
		PageResult pageResult = new PageResult();
		pageResult.setTotal(service.queryPageCount(param));
		pageResult.setRows(list);
		return pageResult;
	}
	
	@ResponseBody
	@RequestMapping(value="/update" , method = { RequestMethod.POST })
	public JsonResult update(DepositRecord record){
		JsonResult jr = JsonResult.newInstance(); 
		Criteria<DepositRecord> param = new Criteria<DepositRecord>();
		param.setRecord(record);
	    String content = null;
	    if (record.getStatus() == RETURN_MAP) {
		    content ="审核不通过"+record.getName()+"提现";
		}else {
			content ="审核通过"+record.getName()+"提现";
			if (record.getId() != null) {
				DepositRecord entity = service.queryEntity(record.getId());
				if (entity != null) {
					UserMoney money = moneyService.queryUserId(entity.getUserId());
					if (money != null) {
						if (entity.getWithdrawMoney() >= 1) {
							if (entity.getWithdrawMoney() <= money.getMoney()) {
								Criteria<UserMoney> params = new Criteria<UserMoney>();
								money.setMoney(money.getMoney() - entity.getWithdrawMoney());
								params.setRecord(money);
								params.addParam("id", money.getId());
								String string = moneyService.update(params);
								if (!StringUtil.isNullOrBlank(string)) {
									return  jr.declareFailure("体现失败!");
								}
							}else {
								return  jr.declareFailure("提现金额不足!");
							}
						}else {
							return  jr.declareFailure("提现金额不能少于1元!"); 
						}
					}
				}
			}
		}
		param.addParam("id", record.getId());
	    String ret = service.update(param);
	    insertLog(content);
		return ret == null ? jr.declareSuccess() : jr.declareFailure(ret); 
	}
}
